'use strict';

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.callAPI = exports.Request = undefined;

var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; };

/**
 * 使用async的请求wrapper
 * @param  {String} url
 * @param  {Object} ajax args
 * @return {Promise}
 */

var callAPI = function () {
  var _ref = _asyncToGenerator(regeneratorRuntime.mark(function _callee(endpoint, args) {
    var data;
    return regeneratorRuntime.wrap(function _callee$(_context) {
      while (1) {
        switch (_context.prev = _context.next) {
          case 0:
            // 某些系统不能识别json object
            if (args.data && !/FormData/.test(args.data.toString()) && !args.keepJs) {
              console.log("tranfer data to string");
              args.data = JSON.stringify(args.data);
            }
            _context.prev = 1;
            _context.next = 4;
            return Request(_extends({}, args, { url: endpoint }));

          case 4:
            data = _context.sent;
            _context.next = 10;
            break;

          case 7:
            _context.prev = 7;
            _context.t0 = _context['catch'](1);
            throw _context.t0;

          case 10:
            return _context.abrupt('return', data);

          case 11:
          case 'end':
            return _context.stop();
        }
      }
    }, _callee, this, [[1, 7]]);
  }));

  return function callAPI(_x, _x2) {
    return _ref.apply(this, arguments);
  };
}();

var _jquery = require('jquery');

var _jquery2 = _interopRequireDefault(_jquery);

var _lodash = require('lodash');

var _lodash2 = _interopRequireDefault(_lodash);

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

function _asyncToGenerator(fn) { return function () { var gen = fn.apply(this, arguments); return new Promise(function (resolve, reject) { function step(key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { return Promise.resolve(value).then(function (value) { return step("next", value); }, function (err) { return step("throw", err); }); } } return step("next"); }); }; }

// import fetch from 'isomorphic-fetch';

/*
*  request args ,jquery ajax的args
*  { url, method, header, data ...}
*/
function Request(args) {
  if (!args.url) {
    console.error("url不能为空");
    return;
  }

  var baseConfig = {
    contentType: 'application/json',
    timeout: 1000 * 10
  };

  //如果用户没设置下面两个属性则自动设置,如果有设置,将被用户的设置覆盖
  if (args.credentials && args.credentials != 'include') {
    delete args.credentials;
    baseConfig.xhrFields = {
      withCredentials: true
    };
    baseConfig.crossDomain = true;
  }

  var finalConfig = _extends({}, baseConfig, args);

  return new Promise(function (resolve, reject) {
    console.log('[RESTACK] sending request to: ' + finalConfig.url);
    // fetch(args.url, { args.method, args.body, args.credentials, args.headers })
    // .then(function(response){
    //   console.log('[RESTACK] request success')
    //   if(response.code == 200 || !response.code) {//没有code 那就把200全当做成功
    //     resolve(response)
    //   }else{
    //     reject(response)
    //   }
    // });
    // .catch(function(error){
    //   console.warn(err)
    //   reject(error);
    // })
    _jquery2.default.ajax(finalConfig).then(function (data, textStatus) {
      console.log('[RESTACK] request success');
      if (data.code == 200 || !data.code) {
        //没有code 那就把200全当做成功
        resolve(data);
      } else {
        reject({ data: data, textStatus: textStatus });
      }
    }, function (err, textStatus) {
      console.warn(err);
      reject(err);
    });
  });
}exports.Request = Request;
exports.callAPI = callAPI;
exports.default = callAPI;